<div class="row animate fadeIn">
    <form id="addForm" class="form-horizontal"  method="post">
        <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Add</i></h5>
            <div class="ibox-tools">
                <button type="button" id="savePayment" class="btn btn-sm btn-primary save-btn-tab">Save</button>
                <a id="cancel_add" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a>
                <a id="done_btn" style="display: none;" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Finish</a>
            </div>
        </div>
        <div class="ibox-content">
            <div class="form-group">
              <label class="col-lg-3 control-label">Prepayment No.</label>
                <div class="col-lg-5">
                  <p class="form-control-static" id="prepayment_no"></p>
                </div>
            </div>
            <div class="form-group">
              <label class="col-lg-3 control-label">Account</label>
                <div class="col-lg-5">
                  <select class="form-control m-b chosen" name="account_id">
                      <option value="">--</option>
                      <?php foreach ($account as $a):?>
                          <option value ="<?php echo $a->account_id; ?>"><?php echo '('.$a->acct_num.') '.$a->acct_name; ?></option>
                      <?php endforeach; ?>
                  </select>
                </div>
            </div>
            <div class="form-group">
              <label class="col-lg-3 control-label">Payment Type</label>
                <div class="col-lg-5">
                  <select class="form-control m-b chosen" name="payment_type">
                      <option value="">--</option>
                      <?php foreach ($paytype as $a):?>
                          <option value ="<?php echo $a->lookup_code; ?>"><?php echo $a->meaning; ?></option>
                      <?php endforeach; ?>
                  </select>
                </div>
            </div>
            <div class="form-group">
              <label class="col-lg-3 control-label"> Cheque No. / Doc No. /</label>
                <div class="col-lg-5">
                  <input type="number" name="doc_no" class="form-control" style="text-align: right;">
                </div>
            </div>
            <div class="form-group">
              <label class="col-lg-3 control-label">Ref No.</label>
                <div class="col-lg-5">
                  <input type="number" name="ref_no" class="form-control" style="text-align: right;">
                </div>
            </div>
            <div class="form-group">
              <label class="col-lg-3 control-label">Amount</label>
                <div class="col-lg-5">
                  <input type="number" name="amount" class="form-control" style="text-align: right;">
                </div>
            </div>
          </div>
    </form>
</div>
      <script>
            $(document).ready(function () {
              $(".chosen").chosen({width:"100%;"});
              $(".chosen1").chosen({width:"100%;","disable_search": true});
              $('.i-checks').iCheck({
                    checkboxClass: 'icheckbox_square-green',
                    radioClass: 'iradio_square-green',
              });

               $("#savePayment").click(function(){
                 if($("#addForm").valid() == true ){
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax').'/insert'); ?>",
                        data: $('#addForm').serialize(),
                        dataType: 'json',
                        success: function(data){
                          if(data.err == 1){
                            $("#savePayment").removeAttr('disabled');
                            notifymsg("danger",data.msg);
                          }else{
                            notifymsg("success","Added Successfully!");
                            $('#addForm #prepayment_no').text(data.payment_no);
                            $("#addForm #done_btn").show();
                            $("#addForm #cancel_add").hide();
                          }
                        },
                        beforeSend: function(){
                           $("#savePayment").attr('disabled',true);
                        }
                    });
                 }
               });

               $("#addForm").validate({
                     rules: {
                         account_id: {
                             required: true
                         },
                         amount: {
                             required: true
                         },
                         payment_type: {
                          required: true
                         }
                     }
                });

            $('#cancel_add').click(function () {
                              swal({
                                          title: "Are you sure?",
                                          text: "Your work will not be save",
                                          type: "warning",
                                          showCancelButton: true,
                                          confirmButtonColor: "#DD6B55",
                                          confirmButtonText: "Leave",
                                          cancelButtonText: "Stay",
                                          closeOnConfirm: true,
                                          closeOnCancel: true },
                                      function (isConfirm) {
                                          if (isConfirm) {
                                            swal("success");
                                              close_add(); active_search();
                                              notifymsg("hide","hide");
                                          } else {
                                              swal("Cancelled");
                                          }
                                      });
                          });

            });
        $('#addX').click(function(){
            close_add();
       });
        $('#done_btn').click(function(){
            close_add();
       });
</script>